import json
import twitter

follow_list = "following.json"

username = 'put_username_here'

# Put your OAuth credentials here. I already had some I could use,
# so haven't investigated putting in a way to do OAuth handshake here

api = twitter.Api(consumer_key='=======',
                  consumer_secret='=======', 
                  access_token_key='=======', 
                  access_token_secret='=======')

following = set()
friends = []
idx = -1   # twitter paging (only gives 100 follows a call), starts at pg1 not 0

while True:
    print "Loading page %s, current following count is %s" % (idx, len(following))
    f, p, n = api.GetFriends(user=username, cursor = idx)
    print "Loaded - prev cursor: %s, next: %s" % (p,n)
    if f.__len__() == 0:
        break
    for friend in f:
        f_params = json.loads(str(friend))
        if f_params['id'] not in following:
            friends.append(f_params)
            following.add(f_params['id'])
    idx = n
    print len(following)

print "Following %s people, saving to %s" % (len(following), follow_list)
json.dump({'ids':list(following), 'friends':friends}, open(follow_list, "w"))
